#include <jni.h>
#include <Hilog/log.h>
#include <GLES3/gl3.h>
#include "napi/js_native_api.h"
#include "napi/native_common.h"
#include "napi/node_api.h"

#include "uv.h"
#include "plugin_common.h"

static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args , nullptr, nullptr));

    napi_valuetype valuetype0;
    NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

    napi_valuetype valuetype1;
    NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));

    double value0;
    NAPI_CALL(env, napi_get_value_double(env, args[0], &value0));

    double value1;
    NAPI_CALL(env, napi_get_value_double(env, args[1], &value1));

    napi_value sum;
    NAPI_CALL(env, napi_create_double(env, value0+value1, &sum));

    return sum;

}

static napi_value testNapiRun(napi_env env, napi_callback_info info){
    napi_value script,result;
    size_t argc =1;

    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &script, NULL, NULL));

    NAPI_CALL(env, napi_run_script(env, script, &result));

    return result;
}

static napi_value getContext(napi_env env, napi_callback_info info){
    napi_value script,result;
    size_t argc =1;

    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &script, NULL, NULL));

    NAPI_CALL(env, napi_run_script(env, script, &result));

    return result;
}

static napi_value testGetPrototype(napi_env env, napi_callback_info info) {
    LOGE("kee cocos testGetPrototype");
    size_t argc =1;
    napi_value args[1], result;

    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

    NAPI_CALL(env, napi_get_prototype(env, args[0], &result));

    return result;
}

static napi_value testFileRead(napi_env env, napi_callback_info info) {
    LOGE("kee cocos testFileRead");
    FILE *fp = nullptr;
    char s[100];
    fp = fopen("/data/data/com.ex.xcomsample/files/test.txt", "r");
    if (fp) {
        fscanf(fp, "%s", s);
        LOGE("kee cocos fp text = %s", s);
    }
    return nullptr;
}

static napi_value testFileReadUV(napi_env env, napi_callback_info info) {
    LOGE("kee cocos testFileReadUV");
    uv_loop_t* uvloop = nullptr;
    NAPI_CALL(env, napi_get_uv_event_loop(env, &uvloop));

    char data[100];
    unsigned int length=0;
    int64_t offset=0;
    uv_fs_t req;
    int result;
    result = uv_fs_open(uvloop, &req, "/data/data/com.ex.xcomsample/files/test.txt", O_RDWR, 0, nullptr);
    uv_file file_r = (uv_file) req.result;
    uv_fs_req_cleanup(&req);

    if (result < 0 || file_r < 0) {
        LOGE("cocos open Readfile failed!");
    }else{
        LOGE("cocos open Readfile success!");
    }

    const uv_buf_t uvBuf = uv_buf_init(data, 100);
    int readResult = uv_fs_read(uvloop, &req, file_r, &uvBuf, 1, offset, nullptr);
    ssize_t readSize = req.result;
    uv_fs_req_cleanup(&req);
    if (readResult < 0 || readSize < 0) {
        LOGE("cocos Readfile failed! %s");
    }else {
        LOGE("cocos Readfile success! file:%s size:%d", data, size_t(readSize));
    }
    return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{

    napi_property_descriptor desc[] = {
      DECLARE_NAPI_FUNCTION("add",Add),
      DECLARE_NAPI_FUNCTION("testNapiRun",testNapiRun),
      DECLARE_NAPI_FUNCTION("getContext", getContext),
      DECLARE_NAPI_FUNCTION("testGetPrototype", testGetPrototype),
      DECLARE_NAPI_FUNCTION("testFileRead", testFileRead),
      DECLARE_NAPI_FUNCTION("testFileReadUV", testFileReadUV),
    };
    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));

    return exports;
}
EXTERN_C_END


static napi_module demoModule = {
.nm_version =1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = Init,
.nm_modname = "hello",
.nm_priv = ((void*)0),
.reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterModule(void)
{
    napi_module_register(&demoModule);
}